package edu.misaka.moe.servlet;

import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class InitServlet
 */
@WebServlet("/init.servlet")
public class InitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public InitServlet() {
        super();
        
    }
    		boolean flag=false;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Cookie[] micookie=request.getCookies();
		if(micookie!=null&&micookie.length!=0) {
			
			Optional<Cookie> cookie=Arrays.asList(micookie).stream().filter(v->Objects.equals(v.getValue(),"www.nnblog.com")).findFirst();
			if(cookie.isPresent()) {
				flag=true;
			}
//			for(int i=0;i<micookie.length;i++) {
//				if(micookie[i].getValue().equals("www.misaka.com")) {
//					flag=true;
//					break;
//				}
//				
//			}
		}
		if(flag==true) {
			request.getRequestDispatcher("view/main.jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("misaka.jsp").forward(request, response);
		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
